const { login } = require('../utils/common-utils')

describe.skip('密码安全初始页面', () => {
  before(function () {
    cy.login()
  })

  beforeEach(function () {
    // 登录
    login()

    cy.visitMainMenuPwdScy()

    // 数据驱动，读取测试用例数据文件
    cy.fixture('password-security').then((expData) => {
      this.expData = expData
      // 密码安全初始页面
      this.initPage = expData.initPage
      // 默认策略编辑页面
      this.defaultPolicyPage = expData.defaultPolicyPage
    })
  })

  it('密码安全初始页面默认元素', function () {
    // html 页面的 title
    cy.title().should('equal', this.initPage.pageTitle)
    // 页面顶部 title
    cy.getElementAssertText('[class^=styles_title_] h1', this.initPage.tipsTitle)
    // 页面顶部描述
    cy.getElementAssertText('[class^=styles_title_] p', this.initPage.tipsDescription)
    // 创建按钮文案
    cy.getElementAssertText('[class^=styles_buttonBox_] span', this.initPage.createPPwdAuthingBtn)
    // 第一个 tab
    cy.getElementAssertText('.ant-tabs-tab:nth-child(1)', this.initPage.firstTab)
    // 第二个 tab
    cy.getElementAssertText('.ant-tabs-tab:nth-child(2)', this.initPage.secondTab)
  })

  it('密码策略表格-表头', function () {
    cy.get('.ant-table-thead .ant-table-cell').should(($cells) => {
      let cellLength = $cells.length
      // 断言表头字段
      expect(cellLength).to.eq(this.initPage.headCellExpectResult.length)

      // 断言表头每个字段文案
      for (let i = 0; i < $cells.length; i++) {
        expect($cells[i].innerText).to.eq(this.initPage.headCellExpectResult[i])
      }
    })
  })

  it('密码策略表格-默认策略', function () {
    cy.get('.ant-table-tbody tr').should(($trs) => {
      let trLength = $trs.length
      // 默认只有一个
      expect(trLength).to.eq(1)
    })

    // 循环表字段
    cy.get('.ant-table-tbody tr:nth-child(1) td').should(($tds) => {
      let tdLength = $tds.length
      expect(tdLength).to.eq(this.initPage.defaultPolicyExpectResult.length)

      for (let i = 0; i < $tds.length; i++) {
        expect($tds[i].innerText).to.eq(this.initPage.defaultPolicyExpectResult[i])
      }
    })
  })

  it('默认策略-保存', function () {
    cy.xpath('//*[contains(text(),"默认策略")]').click()
    cy.getElementAssertText('p[class^=styles_title_]', this.defaultPolicyPage.pageTitle)
    cy.getElementAssertText('#policyName', this.defaultPolicyPage.policyName)
    cy.getElementAssertText('#policyDescribe', this.defaultPolicyPage.policyDescribe)
    cy.getElementAssertText(
      '[class*=styles_simpleForm_] p[class*=styles_subTitle_]+div',
      this.defaultPolicyPage.policyOwner
    )

    // 循环断言密码强度规则
    cy.wrap(this.defaultPolicyPage.passwordRules).each((rule) => {
      cy.xpath(`//span[text()="${rule.name}"]`).click()
      cy.getElementAssertText('[for=passwordStrength] span:last-child', rule.rule)
    })

    // 保存
    cy.get('button[type = submit]').click()
    cy.getElementAssertText('.ant-notification-notice-content', this.defaultPolicyPage.submitSuccess)
  })
})
